package com.company.woye.taiorcheckquestion.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.company.woye.R;
import com.company.woye.taiorcheckSize.bean.TaiorBean;
import com.company.woye.taiorcheckquestion.bean.QuestionBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class QuestonListviewAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<QuestionBean.DataBean> listBeans;

    public QuestonListviewAdapter(Context context, List<QuestionBean.DataBean> listBeans) {
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.listBeans = listBeans;
    }

    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return listBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHodler;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_question_num, parent, false);
            viewHodler = new ViewHolder(convertView);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHolder) convertView.getTag();
        }

        QuestionBean.DataBean dataBean = listBeans.get(position);
        if (TextUtils.isEmpty(dataBean.getTagCount())){
            dataBean.setTagCount("0");
        }
        viewHodler.ivAdd.setTag(position);
        viewHodler.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               int CuPo=Integer.parseInt(listBeans.get((Integer) viewHodler.ivAdd.getTag()).getTagCount());
               CuPo++;
               listBeans.get((Integer) viewHodler.ivAdd.getTag()).setTagCount(CuPo+"");
               notifyDataSetChanged();
            }
        });
        viewHodler.ivPlus.setTag(position);
        viewHodler.ivPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int CuPo=Integer.parseInt(listBeans.get((Integer) viewHodler.ivPlus.getTag()).getTagCount());
                if (CuPo>0){
                    CuPo--;
                    listBeans.get((Integer) viewHodler.ivPlus.getTag()).setTagCount(CuPo+"");
                    notifyDataSetChanged();
                }else {
                    Toast.makeText(mContext,"不能再减了",Toast.LENGTH_LONG).show();
                }
            }
        });
        viewHodler.tvCode.setText(dataBean.getTagCode());
        viewHodler.tvName.setText(dataBean.getTagName());
        viewHodler.tvNum.setText(dataBean.getTagCount());
        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.tv_code)
        TextView tvCode;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.iv_add)
        ImageView ivAdd;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.iv_plus)
        ImageView ivPlus;
        @BindView(R.id.rl)
        RelativeLayout rl;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
